package com.cy.dda.service.goodsdiligencing.impl;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingVerifyTruthReportAttachmentMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingVerifyTruthReportAuditRecordMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingVerifyTruthReportMapper;
import com.cy.dda.dao.mapper.user.UserMapper;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.goodsdiligencing.GoodsDiligencingReportAttachmentSaveAO;
import com.cy.dda.model.ao.goodsdiligencing.GoodsDiligencingReportAuditAO;
import com.cy.dda.model.ao.goodsdiligencing.VerifyTruthReportSaveAO;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencing;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingVerifyTruthReport;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingVerifyTruthReportAttachment;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingVerifyTruthReportAuditRecord;
import com.cy.dda.model.domain.user.User;
import com.cy.dda.model.enums.YesOrNo;
import com.cy.dda.model.enums.diligencing.DiligencingReportAuditResult;
import com.cy.dda.model.enums.diligencing.DiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.*;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAttachmentVO;
import com.cy.dda.model.vo.goodsdiligencing.GoodsDiligencingReportAuditRecordVO;
import com.cy.dda.model.vo.goodsdiligencing.VerifyTruthReportAdminDetailVO;
import com.cy.dda.model.vo.goodsdiligencing.VerifyTruthReportDetailVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.goodsdiligencing.VerifyTruthReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description 验真报告 接口实现类
 * @Author tanyilin
 * @Date 2019/8/27 17:59
 */
@Service
public class VerifyTruthReportServiceImpl extends BaseService implements VerifyTruthReportService {

    @Autowired
    private GoodsDiligencingVerifyTruthReportMapper verifyTruthReportMapper;
    @Autowired
    private GoodsDiligencingVerifyTruthReportAttachmentMapper verifyTruthReportAttachmentMapper;
    @Autowired
    private GoodsDiligencingVerifyTruthReportAuditRecordMapper verifyTruthReportAuditRecordMapper;
    @Autowired
    private GoodsDiligencingMapper goodsDiligencingMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public VerifyTruthReportDetailVO getVerifyTruthReportDetail(Long goodsDiligencingId) {

        GoodsDiligencingVerifyTruthReport verifyTruthReport = new GoodsDiligencingVerifyTruthReport();
        verifyTruthReport.setGoodsDiligencingId(goodsDiligencingId);
        verifyTruthReport = verifyTruthReportMapper.selectOne(verifyTruthReport);
        if(verifyTruthReport != null) {
            //验真报告已填写
            VerifyTruthReportDetailVO verifyTruthReportDetail = new VerifyTruthReportDetailVO();
            BeanUtils.copyProperties(verifyTruthReport, verifyTruthReportDetail);
            verifyTruthReportDetail.setVerifyTruthReportId(verifyTruthReport.getId());
            return verifyTruthReportDetail;
        }

        return null;
    }

    @Override
    public List<GoodsDiligencingReportAttachmentVO> getVerifyTruthReportAttachmentList(Long verifyTruthReportId, GoodsDiligencingAttachmentType type) {

        List<GoodsDiligencingReportAttachmentVO> list = verifyTruthReportAttachmentMapper.selectVerifyTruthReportAttachment(verifyTruthReportId, type);

        return list;
    }

    @Override
    @Transactional
    public Result<Map<String, Object>> saveVerifyTruthReportAttachment(VerifyTruthReportSaveAO saveAO, Long userId) {

        Date time = new Date();
        Long goodsDiligencingId = saveAO.getGoodsDiligencingId();
        Long verifyTruthReportId = saveAO.getVerifyTruthReportId();

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        if(!(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE.equals(dbGoodsDiligencing.getDiligencingType())
                //&& DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus())
                && GoodsDiligencingStatus.VERIFY_TRUTH_ING.equals(dbGoodsDiligencing.getStatus()))) {
            return Result.fail("当前尽调阶段不可提交验真调查报告图片");
        }

        if(CommonUtils.isEmpty(verifyTruthReportId)) {
            //验真报告ID为空，则说明还未提交过验真报告，则新建一个初始值的验真报告
            GoodsDiligencingVerifyTruthReport newVerifyTruthReport = new GoodsDiligencingVerifyTruthReport();
            newVerifyTruthReport.setGoodsDiligencingId(goodsDiligencingId);
            newVerifyTruthReport.setShippingAddressWithApplyAddress(YesOrNo.YES);
            newVerifyTruthReport.setLivingConditions(LivingConditions.NORMAL);
            newVerifyTruthReport.setConclusion(GoodsDiligencingConclusion.NORMAL);
            newVerifyTruthReport.setUserId(userId);
            newVerifyTruthReport.setCreateTime(time);
            newVerifyTruthReport.setUpdateTime(time);
            verifyTruthReportMapper.insertSelective(newVerifyTruthReport);
            verifyTruthReportId = newVerifyTruthReport.getId();
        }

        //保存送货地址图片
        List<GoodsDiligencingReportAttachmentSaveAO> shippingAddressImages = saveAO.getShippingAddressImages();
        if(!CommonUtils.isEmpty(shippingAddressImages)) {
            List<GoodsDiligencingVerifyTruthReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentSaveAO bean : shippingAddressImages) {
                GoodsDiligencingVerifyTruthReportAttachment verifyTruthReportAttachment = new GoodsDiligencingVerifyTruthReportAttachment();
                BeanUtils.copyProperties(bean, verifyTruthReportAttachment);
                verifyTruthReportAttachment.setVerifyTruthReportId(verifyTruthReportId);
                verifyTruthReportAttachment.setCreateTime(time);
                verifyTruthReportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(verifyTruthReportAttachment);
            }
            //批量新增处理
            verifyTruthReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            verifyTruthReportAttachmentMapper.deleteByUpdateTimeAndType(verifyTruthReportId, GoodsDiligencingAttachmentType.SHIPPING_ADDRESS, time);
        }

        //保存家具照片图片
        List<GoodsDiligencingReportAttachmentSaveAO> furnitureImages = saveAO.getFurnitureImages();
        if(!CommonUtils.isEmpty(furnitureImages)) {
            List<GoodsDiligencingVerifyTruthReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentSaveAO bean : furnitureImages) {
                GoodsDiligencingVerifyTruthReportAttachment verifyTruthReportAttachment = new GoodsDiligencingVerifyTruthReportAttachment();
                BeanUtils.copyProperties(bean, verifyTruthReportAttachment);
                verifyTruthReportAttachment.setVerifyTruthReportId(verifyTruthReportId);
                verifyTruthReportAttachment.setCreateTime(time);
                verifyTruthReportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(verifyTruthReportAttachment);
            }
            //批量新增处理
            verifyTruthReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            verifyTruthReportAttachmentMapper.deleteByUpdateTimeAndType(verifyTruthReportId, GoodsDiligencingAttachmentType.FURNITURE_IMAGE, time);
        }

        //保存居住情况图片
        List<GoodsDiligencingReportAttachmentSaveAO> livingConditionsImages = saveAO.getLivingConditionsImages();
        if(!CommonUtils.isEmpty(livingConditionsImages)) {
            List<GoodsDiligencingVerifyTruthReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentSaveAO bean : livingConditionsImages) {
                GoodsDiligencingVerifyTruthReportAttachment verifyTruthReportAttachment = new GoodsDiligencingVerifyTruthReportAttachment();
                BeanUtils.copyProperties(bean, verifyTruthReportAttachment);
                verifyTruthReportAttachment.setVerifyTruthReportId(verifyTruthReportId);
                verifyTruthReportAttachment.setCreateTime(time);
                verifyTruthReportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(verifyTruthReportAttachment);
            }
            //批量新增处理
            verifyTruthReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            verifyTruthReportAttachmentMapper.deleteByUpdateTimeAndType(verifyTruthReportId, GoodsDiligencingAttachmentType.LIVING_CONDITIONS, time);
        }

        //保存现场在场图片
        List<GoodsDiligencingReportAttachmentSaveAO> sceneImages = saveAO.getSceneImages();
        if(!CommonUtils.isEmpty(sceneImages)) {
            List<GoodsDiligencingVerifyTruthReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentSaveAO bean : sceneImages) {
                GoodsDiligencingVerifyTruthReportAttachment verifyTruthReportAttachment = new GoodsDiligencingVerifyTruthReportAttachment();
                BeanUtils.copyProperties(bean, verifyTruthReportAttachment);
                verifyTruthReportAttachment.setVerifyTruthReportId(verifyTruthReportId);
                verifyTruthReportAttachment.setCreateTime(time);
                verifyTruthReportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(verifyTruthReportAttachment);
            }
            //批量新增处理
            verifyTruthReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            verifyTruthReportAttachmentMapper.deleteByUpdateTimeAndType(verifyTruthReportId, GoodsDiligencingAttachmentType.SCENE_IMAGE, time);
        }

        //保存其他图片
        List<GoodsDiligencingReportAttachmentSaveAO> otherImages = saveAO.getOtherImages();
        if(!CommonUtils.isEmpty(otherImages)) {
            List<GoodsDiligencingVerifyTruthReportAttachment> attachmentDoAddList = new ArrayList<>();
            for(GoodsDiligencingReportAttachmentSaveAO bean : otherImages) {
                GoodsDiligencingVerifyTruthReportAttachment verifyTruthReportAttachment = new GoodsDiligencingVerifyTruthReportAttachment();
                BeanUtils.copyProperties(bean, verifyTruthReportAttachment);
                verifyTruthReportAttachment.setVerifyTruthReportId(verifyTruthReportId);
                verifyTruthReportAttachment.setCreateTime(time);
                verifyTruthReportAttachment.setUpdateTime(time);
                attachmentDoAddList.add(verifyTruthReportAttachment);
            }
            //批量新增处理
            verifyTruthReportAttachmentMapper.insertBatch(attachmentDoAddList);
            //删除无效数据
            verifyTruthReportAttachmentMapper.deleteByUpdateTimeAndType(verifyTruthReportId, GoodsDiligencingAttachmentType.OTHER_IMAGE, time);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("goodsDiligencingId", goodsDiligencingId);
        map.put("verifyTruthReportId", verifyTruthReportId);

        return Result.success("保存成功", map);
    }

    @Override
    @Transactional
    public Result<Map<String, Object>> saveVerifyTruthReport(VerifyTruthReportSaveAO saveAO, Long userId) {

        Date time = new Date();
        Long goodsDiligencingId = saveAO.getGoodsDiligencingId();
        Long verifyTruthReportId = saveAO.getVerifyTruthReportId();
        state(!CommonUtils.isEmpty(verifyTruthReportId), "验真报告ID不可为空");

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        if(!(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE.equals(dbGoodsDiligencing.getDiligencingType())
                //&& DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus())
                && GoodsDiligencingStatus.VERIFY_TRUTH_ING.equals(dbGoodsDiligencing.getStatus()))) {
            return Result.fail("当前尽调阶段不可提交验真调查报告");
        }

        //更新验真报告内容
        GoodsDiligencingVerifyTruthReport verifyTruthReport = new GoodsDiligencingVerifyTruthReport();
        BeanUtils.copyProperties(saveAO, verifyTruthReport);
        verifyTruthReport.setId(verifyTruthReportId);
        verifyTruthReport.setUserId(userId);
        verifyTruthReport.setUpdateTime(time);
        if(YesOrNo.YES.equals(verifyTruthReport.getShippingAddressWithApplyAddress())) {
            verifyTruthReport.setCurrentShippingAddress(" ");
        }
        verifyTruthReportMapper.updateByPrimaryKeySelective(verifyTruthReport);

        //更改尽调报告状态为‘待确认’
        if(DiligencingStatus.PROCESSING.equals(dbGoodsDiligencing.getDiligencingStatus()) || DiligencingStatus.REPORT_RETURN.equals(dbGoodsDiligencing.getDiligencingStatus())) {
            GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
            updateGoodsDiligencing.setId(dbGoodsDiligencing.getId());
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_CONFIRM_WAIT);
            updateGoodsDiligencing.setConclusion(saveAO.getConclusion());
            updateGoodsDiligencing.setUpdateTime(time);
            goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("goodsDiligencingId", goodsDiligencingId);
        map.put("verifyTruthReportId", verifyTruthReportId);

        return Result.success("保存成功", map);
    }

    @Override
    @Transactional
    public void doAuditVerifyTruthReport(GoodsDiligencingReportAuditAO auditAO, Long managerId) {

        Date time = new Date();
        Long goodsDiligencingId = auditAO.getGoodsDiligencingId();

        //先查询在库的 验真报告
        GoodsDiligencingVerifyTruthReport verifyTruthReport = new GoodsDiligencingVerifyTruthReport();
        verifyTruthReport.setGoodsDiligencingId(goodsDiligencingId);
        verifyTruthReport = verifyTruthReportMapper.selectOne(verifyTruthReport);
        state(verifyTruthReport != null, "数据异常，该尽调报告不存在");

        //添加审核记录
        GoodsDiligencingVerifyTruthReportAuditRecord auditRecord = new GoodsDiligencingVerifyTruthReportAuditRecord();
        auditRecord.setVerifyTruthReportId(verifyTruthReport.getId());
        auditRecord.setResult(auditAO.getResult());
        auditRecord.setRemark(auditAO.getRemark());
        auditRecord.setManagerId(managerId);
        auditRecord.setCreateTime(time);
        auditRecord.setUpdateTime(time);
        verifyTruthReportAuditRecordMapper.insertSelective(auditRecord);

        //更改 家具尽调 的状态
        GoodsDiligencing updateGoodsDiligencing = new GoodsDiligencing();
        updateGoodsDiligencing.setId(goodsDiligencingId);
        if(DiligencingReportAuditResult.PASS.equals(auditAO.getResult())) {
            //通过，说明 已验真
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.COMPLETED);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.ALREADY_VERIFY_TRUTH);
        }else if(DiligencingReportAuditResult.RETURN.equals(auditAO.getResult())) {
            //退回，说明还是 验真中
            updateGoodsDiligencing.setDiligencingType(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE);
            updateGoodsDiligencing.setDiligencingStatus(DiligencingStatus.REPORT_RETURN);
            updateGoodsDiligencing.setStatus(GoodsDiligencingStatus.VERIFY_TRUTH_ING);
        }
        updateGoodsDiligencing.setUpdateTime(time);
        goodsDiligencingMapper.updateByPrimaryKeySelective(updateGoodsDiligencing);

    }

    @Override
    public VerifyTruthReportAdminDetailVO getVerifyTruthReportAdminDetail(Long goodsDiligencingId) {

        //先查询在库的 验真报告
        GoodsDiligencingVerifyTruthReport verifyTruthReport = new GoodsDiligencingVerifyTruthReport();
        verifyTruthReport.setGoodsDiligencingId(goodsDiligencingId);
        verifyTruthReport = verifyTruthReportMapper.selectOne(verifyTruthReport);
        if(verifyTruthReport != null) {
            //验真报告存在
            VerifyTruthReportAdminDetailVO detailVO = new VerifyTruthReportAdminDetailVO();
            BeanUtils.copyProperties(verifyTruthReport, detailVO);

            //查找调查员
            User user = userMapper.selectByPrimaryKey(verifyTruthReport.getUserId());
            detailVO.setInvestigatorName(user.getName());

            //查询报告审核记录
            List<GoodsDiligencingReportAuditRecordVO> reportAuditRecordList = verifyTruthReportAuditRecordMapper.selectVerifyTruthReportAuditRecord(verifyTruthReport.getId());
            detailVO.setReportAuditRecordList(reportAuditRecordList);

            //查询报告图片
            List<GoodsDiligencingReportAttachmentVO> reportAttachmentList = verifyTruthReportAttachmentMapper.selectVerifyTruthReportAttachment(verifyTruthReport.getId(), null);
            detailVO.setReportAttachmentList(reportAttachmentList);

            return detailVO;
        }

        return new VerifyTruthReportAdminDetailVO();
    }



}
